package com.baiyang.app.tool;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baiyang.app.tool.net.SendMsgUtil;
import com.baiyang.app.tool.net.Urls;
import com.baiyang.app.ui.bookCity.BookCityAdapter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public abstract class MyAdapter<T> extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<T> mData;
    private int mLayoutRes;

    public List<T> getmData() {
        return mData;
    }

    public void setmData(List<T> mData) {
        this.mData = mData;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> mViews;
        private View item;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mViews = new SparseArray<View>();
            item = itemView;
        }

        public void setmOnItemClickListener(View.OnClickListener onClickListener){
            item.setOnClickListener(onClickListener);
        }
        public void setLongClick(View.OnLongClickListener longClick){
            item.setLongClickable(true);
            item.setOnLongClickListener(longClick);
        }
        public ViewHolder setText(int id, CharSequence text) {
            View view = getView(id);
            if (view instanceof TextView) {
                ((TextView) view).setText(text);
            }
            return this;
        }
        public ViewHolder setPic(int id, Bitmap bitmap){
            View view = getView(id);
            if (view instanceof ImageView) {
                ((ImageView) view).setImageBitmap(bitmap);
            }
            return this;
        }
        public ViewHolder setPic(int id, Uri uri){
            View view = getView(id);
            if (view instanceof ImageView) {
                ((ImageView) view).setImageURI(uri);
            }
            return this;
        }
        /**
         * 从网络上获取图片
         * @param id
         * @param url
         * @return
         */
        public ViewHolder setPic(int id, String url){
            View view = getView(id);
            if (view instanceof ImageView) {
                new SendMsgUtil() {
                    @Override
                    public void deal(Call call, Response response) throws IOException {
                        if(response.isSuccessful()){
                            byte[] result = response.body().bytes();
                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inSampleSize = 4;
                            ByteArrayInputStream in = new ByteArrayInputStream(result);
                            Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
                            // 该方法会到时oom
//                            Bitmap bitmap= BitmapFactory.decodeByteArray(result,0,result.length);//利用BitmapFactory将数据转换成bitmap类型
                            view.post(new Runnable() {
                                @Override
                                public void run() {
                                    ((ImageView) view).setImageBitmap(bitmap);
                                }
                            });
                        }
                    }
                    @Override
                    public void dealFailure(Call call, IOException e) throws IOException {
                        LogUtils.i("getPic","IOException:" + e.getLocalizedMessage());
                    }
                }.sendGet(Urls.IMAGE + url);
            }
            return this;
        }
        public ViewHolder setColor(int id, int color){
            View view = getView(id);
            if (view instanceof ImageView) {
                ((TextView) view).setTextColor(color);
            }
            return this;
        }
        public <T extends View> T getView(int id) {
            T t = (T) mViews.get(id);
            if (t == null) {
                t = (T) item.findViewById(id);
                mViews.put(id, t);
            }
            return t;
        }

    }
    public void add(T data) {
        if (mData == null) {
            mData = new ArrayList<T>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }
    public void addAll(List<T> list) {
        if (mData == null) {
            mData = new ArrayList<T>();
        }
        mData.addAll(list);
        notifyDataSetChanged();
    }
    public void add(int position, T data) {
        if (mData == null) {
            mData = new ArrayList<T>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }
    public void remove(T data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }
    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }
    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutRes,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        bindView(holder, getItem(position));
    }
    /** 定义视图绑定方法 **/
    public abstract void bindView(ViewHolder holder, T obj);

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }

    public MyAdapter(List<T> mData, int mLayoutRes) {
        this.mData = mData;
        this.mLayoutRes = mLayoutRes;
    }
    @Override
    public int getItemCount(){
        return mData.size();
    }
    public T getItem(int position) {
        return mData.get(position);
    }
}